package com.qianfeng.smartdevice.mapper;


//
//                            _ooOoo_  
//                           o8888888o  
//                           88" . "88  
//                           (| -_- |)  
//                            O\ = /O  
//                        ____/`---'\____  
//                      .   ' \\| |// `.  
//                       / \\||| : |||// \  
//                     / _||||| -:- |||||- \  
//                       | | \\\ - /// | |  
//                     | \_| ''\---/'' | |  
//                      \ .-\__ `-` ___/-. /  
//                   ___`. .' /--.--\ `. . __  
//                ."" '< `.___\_<|>_/___.' >'"".  
//               | | : `- \`.;`\ _ /`;.`/ - ` : | |  
//                 \ \ `-. \_ __\ /__ _/ .-` / /  
//         ======`-.____`-.___\_____/___.-`____.-'======  
//                            `=---='  
//  
//         .............................................  
//                  佛祖镇楼            BUG辟易  
//          佛曰:  
//                  写字楼里写字间，写字间里程序员；  
//                  程序人员写程序，又拿程序换酒钱。  
//                  酒醒只在网上坐，酒醉还来网下眠；  
//                  酒醉酒醒日复日，网上网下年复年。  
//                  但愿老死电脑间，不愿鞠躬老板前；  
//                  奔驰宝马贵者趣，公交自行程序员。  
//                  别人笑我忒疯癫，我笑自己命太贱；  


import com.qianfeng.smartdevice.pojo.Area;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * Created by jackiechan on 2020/12/11 09:01
 *
 * @Author jackiechan
 */
public interface AreaMapper {
    @Insert("INSERT INTO areas (areaname,parentid,status) VALUES(#{areaname},#{parentid},#{status})")
    void addArea(Area area);

    @Update(" <script>\n" +
            "\n" +
            "            UPDATE areas\n" +
            "\n" +
            "            <set>\n" +
            "                <if test=\"areaname!=null and areaname!=''\">\n" +
            "                    areaname =#{areaname},\n" +
            "                </if>\n" +
            "                <if test=\"parentid!=null\">\n" +
            "                    parentid =#{parentid},\n" +
            "                </if>\n" +
            "                <if test=\"status!=null  and status != '-100' \">\n" +
            "                    status =#{status},\n" +
            "                </if>\n" +
            "            </set>\n" +
            "        where id = #{id}\n" +
            "\n" +
            "\n" +
            "        </script>")
    void updateArea(Area area);

    @Update("<script>\n" +
            "\n" +
            "            UPDATE areas SET status='0'\n" +
            "       where id in\n" +
            "            <foreach collection=\"ids\" item=\"id\" separator=\",\" open=\"(\" close=\")\" >\n" +
            "\n" +
            "                #{id}\n" +
            "            </foreach>\n" +
            "\n" +
            "\n" +
            "        </script>")
    void deleteAreas(@Param("ids") List<String> ids);

    @Select("select a.* ,ap.areaname parentname from areas a left join areas ap on a.parentid=ap.id WHERE a.id =#{id}")
    Area getAreaById(Long id);


    @Select("select a.* ,ap.areaname parentname from areas a left join areas ap on a.parentid=ap.id WHERE a.parentid =#{id}")
    List<Area> getAreaByParentId(Long id);


    @Select("<script>\n" +
            "\n" +
            "            select a.* ,ap.areaname parentname from areas a left join areas ap on a.parentid=ap.id \n" +
            "            <where>\n" +
            "                <if test=\"exludeId !=null\">\n" +
            "                  and   a.id != #{exludeId}\n" +
            "                </if>\n" +
            "                <if test=\"areaname !=null and areaname!=''\">\n" +
            "                   and a.areaname like  concat('%',#{areanname},'%')\n" +
            "                </if>\n" +
            "                <if test=\"status !=null  and status != '-100'\">\n" +
            "                   and  a.status =#{status}\n" +
            "                </if>\n" +
            "\n" +
            "            </where>\n" +
            "\n" +
            "\n" +
            "        </script>")
    List<Area> getAllAreas(@Param("exludeId") String exludeId,@Param("areaname") String areanname,@Param("status") int status);


    /**
     * 查询一级区域
     * @param status
     * @return
     */
    @Select("  <script>\n" +
            "\n" +
            "            SELECT * from areas\n" +
            "           WHERE parentid  is null\n" +
            "        <if test=\"status !=null  and status != '-100'\">\n" +
            "           and  status =#{status}\n" +
            "        </if>\n" +
            "\n" +
            "\n" +
            "        </script>")
    List<Area> getAllFirstParentAreas(@Param("status") int status);

    @Select("SELECT * from areas")
    List<Area> findAllAreas();
}
